package com.xuanmiao.smsback.web.controller.operator;

import com.xuanmiao.smsback.domain.qo.AreaContactInfoQO;
import com.panshi.hujin2.base.common.factory.MessageFactory;
import com.panshi.hujin2.base.common.util.DozerUtils;
import com.panshi.hujin2.base.domain.result.BasicResult;
import com.panshi.hujin2.base.domain.result.BasicResultCode;
import com.xuanmiao.smsback.web.controller.vo.AreaContactInfoVO;

import com.xuanmiao.smsback.model.AreaContactInfoBO;
import com.xuanmiao.smsback.service.platform.operator.AreaContactInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@Api("地区营运配置")
@RequestMapping(value = "/areaContactInfo")
@RestController
public class AreaContactInfoController {
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());
    @Autowired
    private AreaContactInfoService areaContactInfoService;

    @ApiOperation("获取地区联系方式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countryCallingCode" ,value = "国际区号" ,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "nameInChinese" ,value = "国家中文名" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "nameInEnglish" ,value = "国家英文名" ,dataType = "String",paramType = "query"),

    })
    @GetMapping()
    BasicResult<List<AreaContactInfoVO>> all(@ApiIgnore AreaContactInfoQO areaContactInfoQO){

        try {
            List<AreaContactInfoBO> listBOs= areaContactInfoService.getList(areaContactInfoQO);
            List<AreaContactInfoVO> listVOs = Collections.emptyList();
            if (listBOs.size() > 0) {
                listVOs = DozerUtils.convertList(listBOs, AreaContactInfoVO.class);
            }
            return BasicResult.ok(listVOs,areaContactInfoQO.getPage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("主键查询地区数据")
    @GetMapping("{id:\\d+}")
    BasicResult<AreaContactInfoVO> queryPrimaryKey(@PathVariable Integer id) {
        try {
            AreaContactInfoBO res = areaContactInfoService.getById(id);
            return BasicResult.ok(DozerUtils.convert(res, AreaContactInfoVO.class));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("地区联系方式更新")
    @PutMapping("/update")
    BasicResult<Void> update(@Valid @RequestBody AreaContactInfoVO areaContactInfoVO) {
        try {
            AreaContactInfoBO res = areaContactInfoService.getById(areaContactInfoVO.getId());
            if (res == null) {
                return BasicResult.error(BasicResultCode.DATA_NOT_EXIST.getCode(), MessageFactory.getMsg(BasicResultCode.DATA_NOT_EXIST.getMessage()));
            }
            areaContactInfoService.update(DozerUtils.convert(areaContactInfoVO, AreaContactInfoBO.class));
            return BasicResult.ok();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.DATA_NOT_EXIST.getCode(), MessageFactory.getMsg(BasicResultCode.DATA_NOT_EXIST.getMessage()));
        }
    }
}
